<?php

namespace App\Models;


use App\Models\Concerns\ScopeFilterable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
 * Class WishList - 心愿列表
 *
 * @property int $is_realized
 * @property User $user
 * @package App\Models
 */
class WishList extends Model
{
    protected $table = 'wish_lists';

    protected static $unguarded = true;

    use ScopeFilterable;

    const NOT_REALIZED = 0;
    const IS_REALIZED = 1;

    protected $casts = [
        'user_id'     => 'int',
        'is_realized' => 'int',
    ];

    protected $dates = ['realized_at'];

    /**
     * @used-by $user
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    public function realize(): bool
    {
        return $this->update([
            'is_realized' => self::IS_REALIZED,
            'realized_at' => now()
        ]);
    }
}
